package com.zpc.myvideoserver.common.config;

import io.swagger.annotations.ApiOperation;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.oas.annotations.EnableOpenApi;
import springfox.documentation.service.AuthorizationScope;
import springfox.documentation.service.Contact;
import springfox.documentation.service.HttpAuthenticationScheme;
import springfox.documentation.service.SecurityReference;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.contexts.SecurityContext;
import springfox.documentation.spring.web.plugins.Docket;

import java.util.Arrays;
import java.util.Collections;


/**
 * @ClassName : Swagger3Config  //类名
 * @Description : Swagger3配置类  //描述
 * @Author : zpc20 //作者
 * @Date: 2023/11/3  21:11
 */
@Configuration
@EnableOpenApi
public class Swagger3Config {

    @Bean
    public Docket docket(){
        return new Docket(DocumentationType.OAS_30)
                // .enable()
                .securitySchemes(Collections.singletonList(tokenScheme()))
                .securityContexts(Collections.singletonList(tokenContext()))
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.zpc.myvideoserver.controller"))
                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                .paths(PathSelectors.any())//设置paths方法配置路径
                .build()
                //配置文档基本信息
                .apiInfo(new ApiInfoBuilder()
                        .description("My-Video-Project API测试文档")
                        .version("1.0.1")//版本
                        .contact(new Contact("zpc","http://localhost:8086","Zhupc1024@163.com"))
                        .title("My-Video-Project API测试文档")
                        .build()
                );
    }
    // @Description : 配置验证参数 zpc20 2023/11/4 0:17
    private HttpAuthenticationScheme tokenScheme(){
        return HttpAuthenticationScheme.JWT_BEARER_BUILDER.name("Authorization").build();
    }
    // @Description : Token上下文全局配置 zpc20 2023/11/4 0:18
    private SecurityContext tokenContext(){
        return SecurityContext.builder()
                .securityReferences(Collections.singletonList(
                        SecurityReference.builder()
                                .scopes(new AuthorizationScope[0])
                                .reference("Authorization")
                                .build()))
                .operationSelector(o->o.requestMappingPattern().matches("/.*"))
                .build();
    }
}
